﻿// ———————————————————————–
// <copyright file="ClientEncounterType.cs" company="The MITRE Corporation">
//    Copyright (c) 2012 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// ClientEncounterType.cs - Library Class for the TEP Message
// Project: IC.NET - TEP Library
//
// Language:    C#, .NET 4.0
// Platform:    Windows 7, VS 2010 Beta 2
// Author:      Lizzie DeYoung MITRE
//              Brian Wilkins  MITRE

/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2012 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-12-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)using System;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using EDXLSharp.EDXLCTLib;

namespace EDXLSharp.EDXLTEPLib
{
  public class ClientEncounterType : ITEPComposableObject
  {
    
    #region Private Member Variables
    private EDXLStringTypeWrapper encounterID;
    private EDXLDateTimeTypeWrapper encounterDateTime;
    private ValueKeyType locationCategory;
    private EDXLLocationType encounterLocation;
    private List<ClientCareType> clientCare;
    private List<ClientTransferType> clientTransfer;
    #endregion //Private Member Variables

    #region Constructors
    
    public ClientEncounterType()
    {
      this.clientCare = new List<ClientCareType>();
      this.clientTransfer = new List<ClientTransferType>();
    }
    
    #endregion // Constructors

    #region Public Accessors

    public EDXLStringTypeWrapper EncounterID
    {
      get { return this.encounterID; }
      set { this.encounterID = value; }
    }

    public EDXLDateTimeTypeWrapper EncounterDateTime
    {
      get { return this.encounterDateTime; }
      set { this.encounterDateTime = value; }
    }

    public ValueKeyType LocationCategory
    {
      get { return this.locationCategory; }
      set { this.locationCategory = value; }
    }

    public EDXLLocationType EncounterLocation
    {
      get { return this.encounterLocation; }
      set { this.encounterLocation = value; }
    }

    public List<ClientCareType> ClientCare
    {
      get { return this.clientCare; }
      set { this.clientCare = value; }
    }

    public List<ClientTransferType> ClientTransfer
    {
      get { return this.clientTransfer; }
      set { this.clientTransfer = value; }
    }

    #endregion // Public Accessors

    #region Public Member Functions
    
      public override void WriteXML(XmlWriter xwriter, string name) {
        this.Validate();
        
        xwriter.WriteStartDocument(false);
        xwriter.WriteStartElement(name);
        
        if (this.encounterID != null)
        {
          this.encounterID.WriteXML(xwriter, "encounterID");
        }

        if (this.encounterDateTime != null)
        {
          this.encounterDateTime.WriteXML(xwriter, "encounterDateTime");
        }

        if (this.locationCategory != null)
        {
          this.locationCategory.WriteXML(xwriter, "locationCategory");
        }

        if (this.encounterLocation != null)
        {
          this.encounterLocation.WriteXML(xwriter, "encounterLocation");
        }

        if (this.ClientCare != null)
        {
          foreach (ClientCareType val in this.ClientCare)
          {
            val.WriteXML(xwriter, "ClientCare");
          }
        }

        if (this.ClientTransfer != null)
        {
          foreach (ClientTransferType val in this.ClientTransfer)
          {
            val.WriteXML(xwriter, "ClientTransfer");
          }
        }

        xwriter.WriteEndElement();
      }
    
      public override void ReadXML(XmlNode rootnode)
      {
        foreach (XmlNode node in rootnode)
        {
          if (String.IsNullOrEmpty(node.InnerText))
          {
            continue;
          }

          switch (node.LocalName)
          {
            case "encounterID":
              if(this.encounterID == null)
              {
                this.encounterID = new EDXLStringTypeWrapper();
              }
              this.encounterID.ReadXML(node);
              break;

            case "encounterDateTime":
              if(this.encounterDateTime == null)
              {
                this.encounterDateTime = new EDXLDateTimeTypeWrapper();
              }
              this.encounterDateTime.ReadXML(node);
              break;

            case "locationCategory":
              if(this.locationCategory == null)
              {
                this.locationCategory = new ValueKeyType();
              }
              this.locationCategory.ReadXML(node);
              break;

            case "encounterLocation":
              if(this.encounterLocation == null)
              {
                this.encounterLocation = new EDXLLocationType();
              }
              this.encounterLocation.ReadXML(node);
              break;

            case "ClientCare":
              if (this.ClientCare == null)
              {
                this.ClientCare = new List<ClientCareType>();
              }
              ClientCareType newval = new ClientCareType();
              this.ClientCare.Add(newval);
              break;

            case "ClientTransfer":
              if (this.ClientTransfer == null)
              {
                this.ClientTransfer = new List<ClientTransferType>();
              }
              ClientTransferType newval1 = new ClientTransferType();
              this.ClientTransfer.Add(newval1);
              break;
          }
        }

        this.Validate();
      }
    
    #endregion // Public Member Functions

    #region Private Member Functions
    
      protected override void Validate()
      {
        if (this.encounterID == null)
        {
          throw new ArgumentNullException("A encounterID element is required. No null values.");
        }

        if (this.encounterDateTime == null)
        {
          throw new ArgumentNullException("A encounterDateTime element is required. No null values.");
        }

        if (this.locationCategory == null)
        {
          throw new ArgumentNullException("A locationCategory element is required. No null values.");
        }

        if (this.encounterLocation == null)
        {
          throw new ArgumentNullException("A encounterLocation element is required. No null values.");
        }

        if (this.clientCare.Count < 1)
        {
          throw new ArgumentException("There must be at least one clientCare element");
        }
      }
      
    #endregion // Private Member Functions

  }
}
